<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>02组件中的data为什么是函数</title>
</head>

<body>
    <div id="app">
        <cpn></cpn>
        <cpn></cpn>
        <cpn></cpn>
    </div>

    <template id="cpn">
        <div>
            <h2>当前计数:{{count}}</h2>
            <button @click="increment">+</button>
            <button @click="decrement">-</button>
        </div>
    </template>

    <script src="../vue/vue.js"></script>
    <script>
        Vue.component('cpn', {
            template: '#cpn',
            //如果不是函数,每次调用组件都不会返回新的对象,就无法使得每个组件都有自己的数据.
            //这样导致的后果是每个组件都用同一份数据,这样组件的灵活性就没了.
            data() {
                return {
                    count: 0,
                }
            },
            methods: {
                increment(){
                    this.count++;
                },
                decrement(){
                    this.count--;
                }
            },
        })
        const app = new Vue({
            el: '#app'
        });
    </script>
</body>

</html>